package com.xiaorun.portal.controller;

import com.github.pagehelper.PageInfo;
import com.xiaorun.portal.constant.WebConst;
import com.xiaorun.portal.modal.Bo.Page;
import com.xiaorun.portal.modal.Bo.RestResponseBo;
import com.xiaorun.portal.modal.Vo.*;
import com.xiaorun.portal.service.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * 首页
 * Created by Administrator on 2018/11/27 008.
 */
@Controller
public class ZchController extends BaseController {
    @Resource
    private IEnterpriseService enterpriseService;

    @Autowired
    private ICarouselService carouselService;

    @Autowired
    private INewsService newsService;

    @Autowired
    private INewsAttachService newsAttachService;

    @Autowired
    private ICaseService caseService;

    @Autowired
    private INoticeService noticeService;

    @Autowired
    private IProjectService projectService;
    /**
     * 名牌认定
     */
    @GetMapping(value = {"/identification", "/identification.html"})
    public String identification(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>(1);
        params.put("type", WebConst.IDENTIFICATION_CAROUSELS);
        List<Carousel> carousels = carouselService.list(params);
        request.setAttribute("carousels", carousels);

        params.put("type", WebConst.IDENTIFICATION_PARTNER);
        List<Carousel> partners = carouselService.list(params);
        request.setAttribute("partners", partners);

        return this.render("identification");
    }

    /**
     * 名牌认定查询
     */
    @RequestMapping (value = "/enterprise/list/{pageNum}/{pageSize}")
    @ResponseBody
    public PageInfo<Enterprise> listEnterprise(@PathVariable int pageNum, @PathVariable int pageSize, String field, String keyword, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>(1);
        params.put(field, keyword);
        PageInfo<Enterprise> page =  enterpriseService.list(pageNum, pageSize, params);
        return page;
    }

    /**
     * 项目申报
     */
    @GetMapping(value = {"/application", "/application.html"})
    public String application(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>(1);
        params.put("type", WebConst.APPLICATION_CAROUSELS);
        List<Carousel> carousels = carouselService.list(params);
        request.setAttribute("carousels", carousels);

        PageInfo<News> page = newsService.list(1, 8, null);
        List<News> newsList = page.getList();
        request.setAttribute("newsList", newsList);

        List<Project> projects = projectService.list(null);
        request.setAttribute("projects", projects);

        return this.render("application");
    }

    /**
     * 申报资讯
     */
    @RequestMapping(value = "/application/information")
    public String information( String req, HttpServletRequest request) {
        return this.information(1, 10,req, request);
    }

    @RequestMapping(value = "/application/information/{pageNum}/{pageSize}")
    public String information(@PathVariable int pageNum, @PathVariable int pageSize,  String req, HttpServletRequest request) {

         Map<String, Object> params = new HashMap<>(16);
        if(StringUtils.isNotBlank(req)){
            String[] ps = req.split(",");

            for(int i = 0; i < ps.length; i++){
                if(StringUtils.isNotBlank(ps[i])){
                    String[] p = ps[i].split("_");
                    if(p.length >1){
                        if("TWO".equals(p[1])){
                            params.put(p[0],"资金+政策");
                        }else{
                            params.put(p[0],p[1]);
                        }

                    }else{
                        params.put(p[0], "");
                    }
                }
            }
        }

        PageInfo<News> page = newsService.list(pageNum, pageSize, params);
        request.setAttribute("page", page);
        request.setAttribute("pageNum", pageNum);
        request.setAttribute("pageSize", pageSize);

        StringBuilder sb = new StringBuilder();
        if(null!=params && params.size()>0){
            params.forEach((k, v) ->{
                if(StringUtils.isNotBlank((String)v)){
                    if("资金+政策".equals((String)v)){
                        sb.append(k).append("_").append("TWO").append(",");
                    }else{
                        sb.append(k).append("_").append(v).append(",");
                    }

                }
            });
        }

        request.setAttribute("req", sb.toString());

        return this.render("information");
    }

    /**
     * 申报资讯详情
     */
    @GetMapping(value = "/application/information/{id}")
    public String informationDetail(@PathVariable int id, HttpServletRequest request) {
        News news = newsService.get(id);
        request.setAttribute("news", news);

        Map<String, Object> params = new HashMap<>(1);
        params.put("newsId", id);
        List<NewsAttach> newsAttaches = newsAttachService.list(params);
        request.setAttribute("newsAttaches", newsAttaches);

        return this.render("information-detail");
    }

    /**
     * 税务筹划
     */
    @GetMapping(value = {"/tax", "/tax.html"})
    public String tax(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>(1);
        //轮播图
        params.put("type", WebConst.TAX_CAROUSELS);
        List<Carousel> carousels = carouselService.list(params);
        request.setAttribute("carousels", carousels);

        //成功案例
        params.put("type", WebConst.TAX);
        List<Case> cases = caseService.list(params);
        request.setAttribute("cases", cases);

        //合作伙伴
        params.put("type", WebConst.IDENTIFICATION_PARTNER);
        List<Carousel> partners = carouselService.list(params);
        request.setAttribute("partners", partners);
        return this.render("tax");
    }


    /**
     * 上市融资
     */
    @GetMapping(value = {"/ipo", "/ipo.html"})
    public String ipo(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>(1);

        //轮播图
        params.put("type", WebConst.IPO_CAROUSELS);
        List<Carousel> carousels = carouselService.list(params);
        request.setAttribute("carousels", carousels);

        //服务
        params.put("type", WebConst.IPO_SERVICES);
        List<Carousel> services = carouselService.list(params);
        request.setAttribute("services", services);

        //成功案例
        params.put("type", WebConst.IPO);
        List<Case> cases = caseService.list(params);
        request.setAttribute("cases", cases);

        //合作伙伴
        params.put("type", WebConst.IDENTIFICATION_PARTNER);
        List<Carousel> partners = carouselService.list(params);
        request.setAttribute("partners", partners);

        return this.render("ipo");
    }


    /**
     * 产业用地
     */
    @GetMapping(value = {"/land", "/land.html"})
    public String land(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>(1);
        params.put("type", WebConst.LAND_CAROUSELS);
        List<Carousel> carousels = carouselService.list(params);
        request.setAttribute("carousels", carousels);

        PageInfo<Notice> page = noticeService.list(1, 6, null);
        List<Notice> notices = page.getList();
        request.setAttribute("notices", notices);
        return this.render("land");
    }

    /**
     * 土地公告
     */
    @RequestMapping(value = "/land/notice")
    public String notice( String req, HttpServletRequest request) {
        return this.notice(1, 10,req, request);
    }

    @RequestMapping(value = "/land/notice/{pageNum}/{pageSize}")
    public String notice(@PathVariable int pageNum, @PathVariable int pageSize,  String req, HttpServletRequest request) {

        Map<String, Object> params = new HashMap<>(16);

        if(StringUtils.isNotBlank(req)) {
            params.put("type", req);
        }

        PageInfo<Notice> page = noticeService.list(pageNum, pageSize, params);
        request.setAttribute("page", page);
        request.setAttribute("pageNum", pageNum);
        request.setAttribute("pageSize", pageSize);

        request.setAttribute("req", req == null?"":req);

        return this.render("notice");
    }

    /**
     * 土地公告详情
     */
    @GetMapping(value = "/land/notice/{id}")
    public String noticeDetail(@PathVariable int id, HttpServletRequest request) {
        Notice notice = noticeService.get(id);
        request.setAttribute("notice", notice);
        return this.render("notice-detail");
    }

    /**
     * 关于我们
     */
    @GetMapping(value = {"/about", "/about.html"})
    public String about(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>(1);
        params.put("type", WebConst.ABOUT_CAROUSELS);
        List<Carousel> carousels = carouselService.list(params);
        request.setAttribute("carousels", carousels);
        return this.render("about");
    }

}
